package com.ihome.android.market2.view;

//import android.animation.ObjectAnimator;
//import android.animation.PropertyValuesHolder;
//import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.ihome.android.image.CubeImageView;
import com.ihome.android.image.ImageLoader;
import com.ihome.android.academy.R;
import com.ihome.android.market2.frag.RecommAppFragmentEdu;
import com.ihome.android.market2.info.HomepagecontentEdu;
import com.ihome.android.market2.info.TopTitleEdu;

@SuppressLint("NewApi")
public class MyScaleItem implements OnFocusChangeListener {
	private final String	TAG	= MyScaleItem.class.getSimpleName();
	private RelativeLayout	mRelativeLayout;
	//	private RelativeLayout	fl_content;
	private CubeImageView	mImageView;
	private FrameLayout		mFrameLayout;
	private Context			context;
	private Fragment		fragment;
	private ImageLoader		imageLoader;

	public MyScaleItem(Context context, Fragment fragment, ImageLoader imageLoader) {
		this.context = context;
		this.fragment = fragment;
		this.imageLoader = imageLoader;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.my_scale_view, null);
		mImageView = (CubeImageView) mRelativeLayout.findViewById(R.id.tv);
		//		fl_content = (RelativeLayout) mRelativeLayout.findViewById(R.id.fl_content);
		mFrameLayout = (FrameLayout) mRelativeLayout.findViewById(R.id.fl);
		mRelativeLayout.setClickable(true);
		mRelativeLayout.setFocusable(true);
		mRelativeLayout.setFocusableInTouchMode(true);
		mRelativeLayout.setOnFocusChangeListener(this);

	}

	public void setTvBackground(int resid) {
		mImageView.setBackgroundResource(resid);
	}

	public void setImgSrc(int resid) {
		mImageView.setImageResource(resid);
	}

	public void setImageUrl(String url) {
		mImageView.loadImage(imageLoader, url);
	}

	public void setTextViewLayout(android.view.ViewGroup.LayoutParams params) {
		mImageView.setLayoutParams(params);
	}

	public void setClickListener(OnClickListener onClickListener) {
		mRelativeLayout.setOnClickListener(onClickListener);
	}

	public View getView() {
		return mRelativeLayout;
	}

	public void setLayoutParams(RelativeLayout.LayoutParams params) {
		mRelativeLayout.setLayoutParams(params);
	}

	public void setTag(Object tag) {
		mRelativeLayout.setTag(tag);
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (v.getTag() != null && v.getTag() instanceof HomepagecontentEdu) {
			HomepagecontentEdu info = (HomepagecontentEdu) v.getTag();
			if (fragment instanceof RecommAppFragmentEdu && hasFocus) {
				((RecommAppFragmentEdu) fragment).setFocusedPosition(info.getPosition());
			}
		}

		if (hasFocus) {
			mRelativeLayout.bringToFront();

			//			PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.1f);
			//			PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.1f);
			//			ObjectAnimator scaleAnimation = ObjectAnimator.ofPropertyValuesHolder(mRelativeLayout, pvhX, pvhY);
			//			scaleAnimation.setRepeatMode(ValueAnimator.INFINITE);
			//			scaleAnimation.setDuration(150);
			//			scaleAnimation.start();
			ScaleItemAnim anim = new ScaleItemAnim();
			anim.setParams(1.0F, 1.1F, 1.0F, 1.1F, 150L);
			mRelativeLayout.startAnimation(anim.getAnimation());

			mFrameLayout.setVisibility(View.VISIBLE);
			//			fl_content.setBackgroundResource(R.drawable.scale_item_bg);
		}
		else {
			//			PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(View.SCALE_X, 1);
			//			PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1);
			//			ObjectAnimator scaleAnimation = ObjectAnimator.ofPropertyValuesHolder(mRelativeLayout, pvhX, pvhY);
			//			scaleAnimation.setRepeatMode(ValueAnimator.INFINITE);
			//			scaleAnimation.setDuration(0);
			//			scaleAnimation.start();

			ScaleItemAnim anim = new ScaleItemAnim();
			anim.setParams(1.1F, 1.0F, 1.1F, 1.0F, 0L);
			mRelativeLayout.startAnimation(anim.getAnimation());

			mFrameLayout.setVisibility(View.INVISIBLE);
			//			fl_content.setBackgroundResource(context.getResources().getColor(R.color.transparent));
		}
	}

}
